package com.hangzhou.xyj.ui.adapter.viewholder;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.XYJApplication;
import com.hangzhou.xyj.db.preferences.CommonPreferences;
import com.hangzhou.xyj.model.DynamicModel;
import com.hangzhou.xyj.model.type.EIntentTagType;
import com.hangzhou.xyj.model.type.EUserType;
import com.hangzhou.xyj.ui.widget.CircleImageView;
import com.hangzhou.xyj.ui.widget.CommonFollowButton;
import com.hangzhou.xyj.ui.widget.views.MyImageView;
import com.hangzhou.xyj.ui.widget.views.MyTextView;
import com.hangzhou.xyj.utils.PublicUtil;
import com.hangzhou.xyj.utils.StringUtil;

/**
 * Created by yaohuix on 2016/3/4  11:45
 * <p/>
 * 说明：带图片的ViewHolder
 */
public class DynamicImgViewHolder extends RecyclerView.ViewHolder {
    public LinearLayout mLlMoreMenu;//更多菜单
    CircleImageView mCivImageHeader;//头像
    MyTextView mTvName;//昵称
    MyTextView mTvTime;//时间
    MyTextView mTvLocation;//地点
    MyTextView mTvIntroduce;//简介
    MyTextView mTvTransferNum;//转发数量
    MyTextView mTvCommentNum;//评论数量
    MyTextView mTvLikeNum;//点赞数量
    MyImageView mIvImg1;//图片1
    MyImageView mIvImg2;//图片2
    MyImageView mIvImg3;//图片3
    private Context mContext;
    private CommonFollowButton mFollowBtn;
    private int mFollowId;//关注人的ID

    private int mIntentTag;

    public DynamicImgViewHolder(Context context, View view) {
        super(view);
        mContext = context;
        mTvName = (MyTextView) view.findViewById(R.id.mtv_name);
        mFollowBtn = (CommonFollowButton) view.findViewById(R.id.btn_add_follow);
        mTvTime = (MyTextView) view.findViewById(R.id.mtv_time);
        mTvLocation = (MyTextView) view.findViewById(R.id.mtv_location);
        mTvIntroduce = (MyTextView) view.findViewById(R.id.mtv_my_content);
        mTvTransferNum = (MyTextView) view.findViewById(R.id.mtv_transfer_num);
        mTvCommentNum = (MyTextView) view.findViewById(R.id.mtv_comment_num);
        mTvLikeNum = (MyTextView) view.findViewById(R.id.mtv_like_num);
        mCivImageHeader = (CircleImageView) view.findViewById(R.id.civ_header);
        mIvImg1 = (MyImageView) view.findViewById(R.id.miv_image1);
        mIvImg2 = (MyImageView) view.findViewById(R.id.miv_image2);
        mIvImg3 = (MyImageView) view.findViewById(R.id.miv_image3);
        mLlMoreMenu = (LinearLayout) view.findViewById(R.id.ll_more_menu);

    }

    public void setIntentTag(int intentTag) {
        this.mIntentTag = intentTag;
    }

    public void setData(DynamicModel data) {
        int userId = -1;
        if (StringUtil.isNotEmpty(CommonPreferences.getUserId(mContext))) {
            userId = Integer.valueOf(CommonPreferences.getUserId(mContext));
        }
        //我的动态进入
        if (mIntentTag == EIntentTagType.TAG_MY_ACTIVE.getValue()
                || mIntentTag == EIntentTagType.TAG_MY_COLLECTION.getValue()) {
            mLlMoreMenu.setVisibility(View.VISIBLE);
        } else if (mIntentTag == EIntentTagType.TAG_COMMUNITY_FOCUS_DYNAMIC.getValue() ||
                mIntentTag == EIntentTagType.TAG_MINE_PERSONAL_INFO.getValue() ||
                mIntentTag == EIntentTagType.TAG_PERSONAL_INFO.getValue()
                || data.getUserId() == userId) {
            mFollowBtn.setVisibility(View.GONE);
        } else {
            mFollowId = data.getUserId();
            if (XYJApplication.mFollowIdSet.contains(mFollowId)) {
                mFollowBtn.setVisibility(View.VISIBLE);
                mFollowBtn.mIsFollow = true;
                mFollowBtn.updateView();
            } else {
                mFollowBtn.setFollow(data.getIsFollow(), data.getUserId());
            }
        }
        mCivImageHeader.displayImage(data.getUserHead());
        mTvName.setText(data.getUserName());
        if (mIntentTag == EIntentTagType.TAG_MY_COLLECTION.getValue()) {
            mTvTime.setText(data.getCreateTime2());
        } else {
            mTvTime.setText(data.getCreateTime());
        }
        mTvLocation.setText(data.getLocation());
        if (PublicUtil.getGroupInvite(data.getSongName()) != null) {
            String[] part = PublicUtil.getGroupInvite(data.getSongName());
            mTvIntroduce.setNoClickText(part[0] + part[1]);
        } else {
            mTvIntroduce.setNoClickText(data.getSongName());
        }
        mTvTransferNum.setText("" + data.getTransferNum());
        mTvCommentNum.setText("" + data.getCommentNum());
        mTvLikeNum.setText("" + data.getLikeNum());

        int imageListSize = data.getImageList().size();
        if (imageListSize == 1) {
            mIvImg1.displayImage(data.getImageList().get(0));
            mIvImg1.setVisibility(View.VISIBLE);
            mIvImg2.setVisibility(View.INVISIBLE);
            mIvImg3.setVisibility(View.INVISIBLE);
        } else if (imageListSize == 2) {
            mIvImg1.displayImage(data.getImageList().get(0));
            mIvImg2.displayImage(data.getImageList().get(1));
            mIvImg1.setVisibility(View.VISIBLE);
            mIvImg2.setVisibility(View.VISIBLE);
            mIvImg3.setVisibility(View.INVISIBLE);
        } else {
            mIvImg1.displayImage(data.getImageList().get(0));
            mIvImg2.displayImage(data.getImageList().get(1));
            mIvImg3.displayImage(data.getImageList().get(2));
            mIvImg1.setVisibility(View.VISIBLE);
            mIvImg2.setVisibility(View.VISIBLE);
            mIvImg3.setVisibility(View.VISIBLE);
        }
        //设置昵称右边的认证标志
        EUserType userType = PublicUtil.getUserType(data.getType(), data.getCheckFlag());
        mTvName.setCompoundDrawablesWithIntrinsicBounds(null, null, PublicUtil.getAuthenIconDrawable(mContext, userType), null);
    }

}
